<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>Jessibuca Pro 点播(sm4) demo</title>
    <script src="./vconsole.js"></script>
    <script src="./js/jessibuca-pro-demo.js"></script>
    <link rel="stylesheet" href="./demo.css">
    <style>
        .container-shell:before {
            content: "jessibuca pro 点播 demo";
        }
    </style>
</head>
<body class="page">
<div class="root">
    <div class="container-shell">
        <div id="container"></div>
        <div class="input">
            <div>
                当前浏览器：
                <span id="mseSupport264" style="color: green;display: none">支持MSE H264解码；</span>
                <span id="mseSupport" style="color: green;display: none">支持MSE H265解码；</span>
                <span id="mseNotSupport264" style="color: red;display: none">不支持MSE H264解码；</span>
                <span id="mseNotSupport"
                      style="color: red;display: none">不支持MSE H265解码,会自动切换成wasm解码；</span>
            </div>
        </div>
        <div class="input">
            <div>
                当前浏览器：
                <span id="wcsSupport264" style="color: green;display: none">支持Webcodecs H264解码；</span>
                <span id="wcsSupport" style="color: green;display: none">支持Webcodecs H265解码（不一定准确）；</span>
                <span id="wcsNotSupport264"
                      style="color: red;display: none">不支持Webcodecs H264解码(https/localhost)；</span>
                <span id="wcsNotSupport" style="color: red;display: none">不支持Webcodecs H265解码(https/localhost),会自动切换成wasm解码</span>
            </div>
        </div>
        <div class="input">
            <div>
                当前浏览器：
                <span id="wasmSupport" style="color: green;display: none">支持WASM解码；</span>
                <span id="simdSupport" style="color: green;display: none">支持WASM SIMD解码</span>
                <span id="wasmNotSupport" style="color: red;display: none">不支持WASM解码</span>
                <span id="simdNotSupport"
                      style="color: red;display: none">不支持WASM SIMD解码,会自动切换成wasm解码</span>
            </div>
        </div>

        <div class="input">
            <div>
                解码器：
                <input
                    checked
                    type="checkbox"
                    id="useMSE"
                /><span>MediaSource</span>
                <input
                    type="checkbox"
                    id="useWCS"
                /><span>Webcodec</span>
                <input
                    type="checkbox"
                    id="useSIMD"
                /><span>WASM(SIMD)</span>
            </div>
        </div>
        <div class="input">

            <div>
                <input
                    type="checkbox"
                    id="isMP4"
                /><span>强制MP4</span>
                <input
                    checked
                    onclick="replay()"
                    type="checkbox"
                    id="m7sCryptoAudio"
                /><span>解密音频数据</span>
                <span>秘钥：</span>
                <input type="text" id="m7sKey" style="width: 200px" placeholder="请输入秘钥" value="">
            </div>

            <button onclick="replay()">重播</button>
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl"
                value=""
            />
        </div>
        <div class="input">
            <button id="play">播放</button>
            <button id="playVodPause">停止</button>
        </div>
        <div class="input">
            <button id="playVodForward">快进10s</button>
            <button id="playVodBackword">快退10s</button>
        </div>
        <div class="input" style="line-height: 30px">
            <button id="destroy">销毁</button>
        </div>
    </div>
</div>
<script src="./demo.js"></script>

<script>
    var $player = document.getElementById('play');
    var $playVodPause = document.getElementById('playVodPause');
    var $playHref = document.getElementById('playUrl');
    var $container = document.getElementById('container');
    var $destroy = document.getElementById('destroy');
    var $useMSE = document.getElementById('useMSE');
    var $useSIMD = document.getElementById('useSIMD');
    var $useWCS = document.getElementById('useWCS');
    var $playVodForward = document.getElementById('playVodForward');
    var $playVodBackword = document.getElementById('playVodBackword');
    var $m7sKey = document.getElementById('m7sKey');
    var $m7sCryptoAudio = document.getElementById('m7sCryptoAudio');
    var $isMP4 = document.getElementById('isMP4');

    var forceNoOffscreen = true; //
    var jessibuca = null;
    var isPlayVodPause = false;


    function create() {
        const showOperateBtns = true;
        jessibuca = new JessibucaPro({
            playType: 'playVod',
            container: $container,
            videoBuffer: 0.2, // 缓存时长
            decoder: './js/decoder-pro.js',
            isResize: false,
            text: "",
            loadingText: "加载中",
            isNotMute: true,
            debug: true,
            debugLevel: 'debug',
            timeout: 10000,
            // showPerformance: true,
            showBandwidth: showOperateBtns, // 显示网速
            showPlaybackOperate: showOperateBtns,
            operateBtns: {
                fullscreen: showOperateBtns,
                screenshot: showOperateBtns,
                play: showOperateBtns,
                audio: showOperateBtns,
                performance: showOperateBtns,
            },
            playVodConfig: {
                showRateBtn: true,
                rateConfig: [
                    {label: '正常', value: 1},
                    {label: '2倍', value: 2},
                    {label: '3倍', value: 3},
                    {label: '4倍', value: 4},
                    {label: '5倍', value: 5},
                    {label: '6倍', value: 6},
                    {label: '7倍', value: 7},
                    {label: '8倍', value: 8},
                    {label: '9倍', value: 9},
                    {label: '10倍', value: 10},
                    {label: '11倍', value: 11},
                    {label: '12倍', value: 12},
                    {label: '13倍', value: 13},
                    {label: '14倍', value: 14},
                    {label: '15倍', value: 15},
                    {label: '16倍', value: 16},
                ],
                useWCS: $useWCS.checked,
                useMSE: $useMSE.checked,
                useSIMD: $useSIMD.checked,
            },
            forceNoOffscreen: forceNoOffscreen,
            isSm4Crypto: true,
            sm4CryptoKey: $m7sKey.value,
            isCryptoAudio: $m7sCryptoAudio.checked === true,
            isFmp4: $isMP4.checked,
        },);

        jessibuca.onLog = msg => console.error(msg);
        jessibuca.onRecord = (status) => console.log('onRecord', status);
        jessibuca.onPause = () => console.log('onPause');
        jessibuca.onPlay = () => console.log('onPlay');
        jessibuca.onFullscreen = msg => console.log('onFullscreen', msg);
        jessibuca.onMute = msg => console.log('onMute', msg);
        $player.style.display = 'inline-block';
        $playVodPause.style.display = 'none';
        $destroy.style.display = 'none';
    }


    create();

    function replay() {
        if (jessibuca) {
            jessibuca.destroy().then(() => {
                create();
                play();
            });
        } else {
            create();
            play();
        }
    }

    function play() {
        var href = $playHref.value;

        if (!href) {
            return
        }

        if (isPlayVodPause) {
            jessibuca.playVodResume()
        } else {
            jessibuca.playVod(href).then(() => {
                notifySuccess('播放成功');
            }).catch((error) => {
                jessibuca.showErrorMessageTips(error.toString());
            });
        }
        isPlayVodPause = false;
        $player.style.display = 'none';
        $playVodPause.style.display = 'inline-block';
        $destroy.style.display = 'inline-block';
    }


    $player.addEventListener('click', function () {
        play();
    }, false)


    $useMSE.addEventListener('click', function () {
        const checked = $useMSE.checked;
        if (checked) {
            $useSIMD.checked = false
            $useWCS.checked = false
        }
        replay();
    })

    $useSIMD.addEventListener('click', function () {
        const checked = $useSIMD.checked;
        if (checked) {
            $useMSE.checked = false
            $useWCS.checked = false
        }
        replay();
    })

    $useWCS.addEventListener('click', function () {
        const checked = $useWCS.checked;
        if (checked) {
            $useMSE.checked = false
            $useSIMD.checked = false
        }
        replay();
    })


    $playVodPause.addEventListener('click', function () {
        $playVodPause.style.display = 'none';
        $player.style.display = 'inline-block';
        jessibuca.playVodPause();
        isPlayVodPause = true;
    })

    $destroy.addEventListener('click', function () {
        if (jessibuca) {
            jessibuca.destroy().then(() => {
                create();
            });
        } else {
            create();
        }

    })

    $playVodForward.addEventListener('click', function () {
        if (jessibuca) {
            jessibuca.playVodForwardJumping(10);
        }
    })

    $playVodBackword.addEventListener('click', function () {
        if (jessibuca) {
            jessibuca.playVodBackwordJumping(10);
        }
    })


</script>

</body>
</html>
